home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / print / gs261sr1.zip / GDEVTKNK.C < prev    next >
C/C++ Source or Header  |  1993-05-12  |  9KB  |  254 lines

  1. /* Copyright (C) 1992 Aladdin Enterprises.  All rights reserved.
  2.  
  3. This file is part of Ghostscript.
  4.  
  5. Ghostscript is distributed in the hope that it will be useful, but
  6. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  7. to anyone for the consequences of using it or for whether it serves any
  8. particular purpose or works at all, unless he says so in writing.  Refer
  9. to the Ghostscript General Public License for full details.
  10.  
  11. Everyone is granted permission to copy, modify and redistribute
  12. Ghostscript, but only under the conditions described in the Ghostscript
  13. General Public License.  A copy of this license is supposed to have been
  14. given to you along with Ghostscript so you can know your rights and
  15. responsibilities.  It should be in a file named COPYING.  Among other
  16. things, the copyright notice and this notice must be preserved on all
  17. copies.  */
  18.  
  19. /* gdevtknk.c */
  20. /*   
  21.    Tektronix Ink-jet plotter driver for Ghostscript.
  22.    This code is written for 4696 and 4695 plotters, it may easily be 
  23.    adopted to the 4393 and 4394 models as well, simply by adding new
  24.    device descriptors with other geometrical characteristics.
  25. */
  26. #include "gdevprn.h"
  27. #include "malloc_.h"
  28.  
  29. /* Thanks to Karsten Spang (spang@nbivax.nbi.dk) for contributing */
  30. /* this code to Ghostscript. */
  31.  
  32.  
  33. /* The device descriptor */
  34. /* We need our own color mapping procedures. */
  35. private dev_proc_map_rgb_color(tekink_map_rgb_color);
  36. private dev_proc_map_color_rgb(tekink_map_color_rgb);
  37. private dev_proc_print_page(tekink_print_page);
  38. private gx_device_procs tekink_procs =
  39.     prn_color_procs(gdev_prn_open, gdev_prn_output_page, gdev_prn_close,
  40.         tekink_map_rgb_color, tekink_map_color_rgb);
  41.  
  42.  
  43. /* 
  44.    Device descriptor for the Tek 4696.
  45.    The 4696 plotter uses roll media, thus the y size is arbitrary. The
  46.    value below is chosen to make the image area A*-format like, i.e. the 
  47.    aspect ratio is close to sqrt(2).
  48. */
  49. gx_device_printer gs_tek4696_device =
  50.     prn_device(tekink_procs,"tek4696",
  51.     85,120,    /* Page size in 10th of inches */
  52.     120,120,    /* Resolution in DPI */
  53.     0.0,0.0,0.0,0.0,    /* Margins */
  54.     4,        /* Bits per pixel */
  55.     tekink_print_page);
  56.  
  57. /* Color mapping.
  58.    The tek inkjets use subtractive colors B=0 M=1 Y=2 C=3. These are 
  59.    represented as 4 bits B=1 M=2 Y=4 C=8 in a byte. This gives:
  60.       White   =  0
  61.       Black   =  1
  62.       Magenta =  2
  63.       Yellow  =  4
  64.       Red     =  6
  65.       Cyan    =  8
  66.       Blue    = 10
  67.       Green   = 12
  68.    The remaining values are unused. (They give ugly results if sent to the
  69.    plotter.) Of course this could have been compressed into 3 bits, but 
  70.    as the palette color memory device uses 8 bits anyway, this is easier,
  71.    and perhaps faster.
  72. */
  73.  
  74. static gx_color_index rgb_to_index[8]={1,6,12,4,10,2,8,0};
  75. static ushort index_to_rgb[16][3]={
  76.     {65535,65535,65535}, /* White */
  77.     {0,0,0}, /* Black */
  78.     {65535,0,65535}, /* Magenta */
  79.     {2,2,2}, /* Unused */
  80.     {65535,65535,0}, /* Yellow */
  81.     {2,2,2}, /* Unused */
  82.     {65535,0,0}, /* Red */
  83.     {2,2,2}, /* Unused */
  84.     {0,65535,65535}, /* Cyan */
  85.     {2,2,2}, /* Unused */
  86.     {0,0,65535}, /* Blue */
  87.     {2,2,2}, /* Unused */
  88.     {0,65535,0}, /* Green */
  89.     {2,2,2}, /* Unused */
  90.     {2,2,2}, /* Unused */
  91.     {2,2,2}  /* Unused */
  92. };
  93.  
  94. /* Map an RGB color to a printer color. */
  95. private gx_color_index
  96. tekink_map_rgb_color(gx_device *dev, ushort r, ushort g, ushort b)
  97. {
  98.     return(rgb_to_index[(((b>32767) << 2) + ((g>32767) << 1) + 
  99.             (r>32767)) & 7]);
  100. }
  101.  
  102. /* Map the printer color back to RGB. */
  103. private int
  104. tekink_map_color_rgb(gx_device *dev, gx_color_index color, ushort prgb[3])
  105. {
  106.     register ushort c = (ushort)color;
  107.     register int i;
  108.     if (c>15) return -1;
  109.     if (index_to_rgb[c][0]==2) return -1;
  110.     for (i=0;i<3;i++){
  111.         prgb[i]=index_to_rgb[c][i];
  112.     }
  113.     return 0;
  114. }
  115.  
  116. /* Send the page to the printer. */
  117. private int
  118. tekink_print_page(gx_device_printer *pdev,FILE *prn_stream)
  119. {
  120.     int line_size,color_line_size,scan_line,num_bytes,scan_lines,color_plane;
  121.     int roll_paper,out_line,micro_line,pending_micro_lines,line_blank,
  122.         blank_lines;
  123.     byte *outdata,*indata1,*bdata1,*mdata1,*ydata1,*cdata1;
  124.     register byte *indata,*bdatap,*mdatap,*ydatap,*cdatap;
  125.     register byte bdata,mdata,ydata,cdata;
  126.     register byte mask,inbyte;
  127.     register byte *indataend,*outdataend;
  128.     
  129.     /* Allocate a temporary buffer for color separation.
  130.        The buffer is partitioned into an input buffer and four
  131.        output buffers for the color planes. The output buffers
  132.        are allocated with an extra sentinel byte. */
  133.     
  134.     line_size = gdev_mem_bytes_per_scan_line((gx_device *)pdev);
  135.     color_line_size=(pdev->width+7)/8;
  136.     indata1=(byte *)malloc(line_size+4*(color_line_size+1));
  137.     if (indata1==NULL) return -1;
  138.     /* pointers to the partions */
  139.     indataend=indata1+line_size;
  140.     bdata1=indataend;
  141.     mdata1=bdata1+(color_line_size+1);
  142.     ydata1=mdata1+(color_line_size+1);
  143.     cdata1=ydata1+(color_line_size+1);
  144.  
  145.     /* Does this device use roll paper? */
  146.     roll_paper=!strcmp(pdev->dname,"tek4696");
  147.     
  148.     out_line=0;
  149.     blank_lines=0;
  150.     scan_lines=pdev->height;
  151.     for (scan_line=0;scan_line<scan_lines;scan_line++){
  152.         /* get data */
  153.         gdev_prn_copy_scan_lines(pdev,scan_line,indata1,line_size);
  154.         /* Separate data into color planes */
  155.         bdatap = bdata1+1;
  156.         mdatap = mdata1+1;
  157.         ydatap = ydata1+1;
  158.         cdatap = cdata1+1;
  159.         bdata=0;
  160.         mdata=0;
  161.         cdata=0;
  162.         ydata=0;
  163.         mask=0x80;
  164.         memset(indataend,0,4*(color_line_size+1));
  165.         for (indata=indata1;indata<indataend;indata++){
  166.             inbyte = *indata;
  167.             if (inbyte&0x01) bdata|=mask;
  168.             if (inbyte&0x02) mdata|=mask;
  169.             if (inbyte&0x04) ydata|=mask;
  170.             if (inbyte&0x08) cdata|=mask;
  171.             mask>>=1;
  172.             if (!mask){
  173.                 *(bdatap++) = bdata;
  174.                 *(mdatap++) = mdata;
  175.                 *(cdatap++) = cdata;
  176.                 *(ydatap++) = ydata;
  177.                 bdata=0;
  178.                 mdata=0;
  179.                 cdata=0;
  180.                 ydata=0;
  181.                 mask=0x80;
  182.             }
  183.         }
  184.         if (mask!=0x80){
  185.             *bdatap = bdata;
  186.             *mdatap = mdata;
  187.             *cdatap = cdata;
  188.             *ydatap = ydata;
  189.         }
  190.         line_blank=1;
  191.         /* Output each of the four color planes */
  192.         for (color_plane=0;color_plane<4;color_plane++){
  193.             outdata=indataend+(color_plane*(color_line_size+1));
  194.             outdataend=outdata+color_line_size;
  195.                 
  196.             /* Remove trailing spaces and output the color line if it is 
  197.                not blank */
  198.             *outdata=0xff;
  199.             while (!(*outdataend)) outdataend--;
  200.             if (num_bytes=(outdataend-outdata)){
  201.                 line_blank=0;
  202.                 /* On encountering the first non-blank data, output pending
  203.                    blank lines */
  204.                 if (blank_lines){
  205.                     pending_micro_lines=((out_line+blank_lines+1)/4)-
  206.                         (out_line/4);
  207.                     for (micro_line=0;micro_line<pending_micro_lines;
  208.                         micro_line++){
  209.                         fputs("\033A",prn_stream);
  210.                     }
  211.                     out_line+=blank_lines;
  212.                     blank_lines=0;
  213.                 }
  214.                 fprintf(prn_stream,"\033I%c%03d",'0'+(out_line%4)+
  215.                     4*color_plane,num_bytes);
  216.                 fwrite(outdata+1,1,num_bytes,prn_stream);
  217.             }
  218.         } /* loop over color planes */
  219.         
  220.         /* If this line is blank, and if it is a roll paper model,
  221.            count the line. Otherwise output the line */
  222.         if (line_blank&&roll_paper){
  223.             /* Only increment the blank line count, if non blank lines 
  224.                have been encountered previously, i.e. skip leading blank
  225.                lines. */
  226.             if (o